package org.example.myleet.p937;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Solution {
    public String[] reorderLogFiles(String[] logs) {
        List<Log> alphabeticLogs = new ArrayList<>();
        List<Log> numericLogs = new ArrayList<>();
        for (String log : logs) {
            Log l = new Log(log);
            if (LogTypeEnum.ALPHABETIC == l.logType) {
                alphabeticLogs.add(l);
            } else {
                numericLogs.add(l);
            }
        }
        alphabeticLogs.sort(new Comparator<Log>() {
            @Override
            public int compare(Log o1, Log o2) {
                if (!o1.content.equals(o2.content)) {
                    return o1.content.compareTo(o2.content);
                }
                return o1.tag.compareTo(o2.tag);
            }
        });
        alphabeticLogs.addAll(numericLogs);
        String[] result = new String[alphabeticLogs.size()];
        for (int i = 0; i < alphabeticLogs.size(); ++i) {
            result[i] = alphabeticLogs.get(i).holdLog;
        }
        return result;
    }

    public enum LogTypeEnum {
        ALPHABETIC,
        NUMERIC,
        ;
    }

    public static class Log {
        LogTypeEnum logType;
        String tag;
        String content;
        String holdLog;

        public Log(String log) {
            holdLog = log;
            String[] word = log.split(" ");
            tag = word[0];
            content = log.substring(tag.length() + 1);
            if (Character.isDigit(content.charAt(0))) {
                logType = LogTypeEnum.NUMERIC;
            } else {
                logType = LogTypeEnum.ALPHABETIC;
            }
        }
    }
}
